package br.com.fitness.infrastructure;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.com.fitness.core.domain.person.City;
import br.com.fitness.core.domain.person.Person;
import br.com.fitness.core.domain.person.PersonRepository;
import br.com.fitness.core.domain.person.State;

public class PersonRepositoryImpl extends
		AbstractRepositoryImpl<Person, Integer> implements PersonRepository {

	public PersonRepositoryImpl() {
		super(Person.class);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public List<City> findCitiesByState(State state) {

		Criteria criteria = getSession().createCriteria(City.class);

		criteria.add(Restrictions.eq("state", state));
		criteria.addOrder(Order.asc("name"));
		return criteria.list();
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public List<State> listState() {

		Criteria criteria = getSession().createCriteria(State.class);
		criteria.addOrder(Order.asc("name"));
		return criteria.list();
	}

}
